package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.UserLike;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.stream.Collectors;

@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        if (this.mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setCreated(System.currentTimeMillis());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);

        this.mongoTemplate.save(userLike);
        return userLike.getId().toHexString();

    }

    /**
     * 判断是否相互喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        //判断条件
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);

        //或运算符
        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);
        return this.mongoTemplate.count(Query.query(criteria), UserLike.class) == 2;
    }

    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        DeleteResult deleteResult = mongoTemplate.remove(query, UserLike.class);

        return deleteResult.getDeletedCount() == 1;
    }

    @Override
    public Long queryEachLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = this.mongoTemplate.find(query, UserLike.class);

        List<Long> userLikeIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        Query query1 = Query.query(Criteria.where("userId").in(userLikeIds).and("userLikeId").is(userId));
        return this.mongoTemplate.count(query1, UserLike.class);
    }

    @Override
    public Long queryLikeCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public Long queryFanCount(Long userId) {
        Query query = Query.query(Criteria.where("userLikeId").is(userId));
        return this.mongoTemplate.count(query, UserLike.class);
    }

    @Override
    public List<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = this.mongoTemplate.find(query, UserLike.class);

        List<Long> userLikeIds = userLikes.stream().map(UserLike::getLikeUserId).collect(Collectors.toList());

        Query query1 = Query.query(Criteria.where("userId").in(userLikeIds).and("userLikeId").is(userId));
        return this.mongoTemplate.find(query1, UserLike.class);

    }

    @Override
    public List<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        return this.mongoTemplate.find(query, UserLike.class);
    }

    @Override
    public List<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        Query query = Query.query(Criteria.where("userLikeId").is(userId));
        return this.mongoTemplate.find(query, UserLike.class);
    }

}
